package com.lc.tgxm.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Toast;

import com.lc.tgxm.R;

/**
 * Helper class to abstract dialog creation from the rest of the application
 * 产生各种对话框的工具类
 * v1.1[20170116]
 */
public class DialogCommandWrapper implements OnClickListener {
    private final Runnable command;

    public DialogCommandWrapper(Runnable command) {
        this.command = command;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        command.run();
    }

    /**
     * Creates a generic confirmation dialog.
     *
     * @param context     Context to build dialog for
     * @param title       Description title for dialog
     * @param confirmText Text for confirmation button
     * @param dismissText Text for cancel/dismiss button
     * @param command     Runnable object that is invoked when user presses the
     *                    confirmation button
     * @return
     */
    public static Dialog createConfirmationDialog(Context context, String title, String confirmText,
                                                  String dismissText, Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setTitle(title);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        dialogBuilder.setNegativeButton(dismissText, new DialogCommandWrapper(NO_OP));
        return dialogBuilder.create();
    }

    // One button dialog
    public static Dialog createAlertDialog(Context context, String title, String msg, String confirmText,
                                           Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(false);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (title != null)
            dialogBuilder.setTitle(title);
        if (msg != null)
            dialogBuilder.setMessage(msg);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        return dialogBuilder.create();
    }

    // One button dialog no Runnable
    public static Dialog createAlertDialog(Context context, String title, String msg, String confirmText) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(false);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (title != null)
            dialogBuilder.setTitle(title);
        if (msg != null)
            dialogBuilder.setMessage(msg);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(NO_OP));
        return dialogBuilder.create();
    }

    // One button dialog
    public static Dialog createAlertDialog(Context context, int title, int msgId, int confirmText,
                                           Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(false);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (-1 != title)
            dialogBuilder.setTitle(title);
        if (-1 != msgId)
            dialogBuilder.setMessage(msgId);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        return dialogBuilder.create();
    }

    // One button dialog
    public static Dialog createAlertDialog(Context context, int title, String message, int confirmText,
                                           Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(false);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (-1 != title)
            dialogBuilder.setTitle(title);
        if (message != null)
            dialogBuilder.setMessage(message);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        return dialogBuilder.create();
    }

    // One button dialog
    public static Dialog createAlertDialog(Context context, int title, String message, int confirmText,
                                           String etText, Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(false);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (-1 != title)
            dialogBuilder.setTitle(title);
        if (message != null)
            dialogBuilder.setMessage(message);
        EditText et = new EditText(context);
        et.setText(etText);
        dialogBuilder.setView(et);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        return dialogBuilder.create();
    }

    // Two buttons dialog
    public static Dialog createAlertDialog(Context context, String title, String message, String confirmText,
                                           String cancelText, Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (title != null)
            dialogBuilder.setTitle(title);
        if (message != null)
            dialogBuilder.setMessage(message);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        dialogBuilder.setNegativeButton(cancelText, new DialogCommandWrapper(NO_OP));
        return dialogBuilder.create();
    }

    // Two buttons dialog
    public static Dialog createAlertDialog(Context context, int title, int message, int confirmText,
                                           int cancelText, Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (-1 != title)
            dialogBuilder.setTitle(title);
        if (-1 != message)
            dialogBuilder.setMessage(message);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        dialogBuilder.setNegativeButton(cancelText, new DialogCommandWrapper(NO_OP));
        return dialogBuilder.create();
    }

    // Two buttons dialog
    public static Dialog createAlertDialog(Context context, int title, String message, int confirmText,
                                           int cancelText, Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (-1 != title)
            dialogBuilder.setTitle(title);
        dialogBuilder.setMessage(message);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        dialogBuilder.setNegativeButton(cancelText, new DialogCommandWrapper(NO_OP));
        return dialogBuilder.create();
    }

    // Two buttons dialog
    public static Dialog createAlertDialog(Context context, String title, String message, int confirmText,
                                           int cancelText, Runnable command) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (null != title)
            dialogBuilder.setTitle(title);
        dialogBuilder.setMessage(message);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command));
        dialogBuilder.setNegativeButton(cancelText, new DialogCommandWrapper(NO_OP));
        return dialogBuilder.create();
    }

    // Default OK Cancel
    public static Dialog createAlertDialog(Context context, String title, String message, Runnable command) {
        return createAlertDialog(context, title, message, R.string.ok, R.string.cancel, command);
    }

    // Default OK Cancel
    public static Dialog createAlertDialog(Context context, int title, int message, Runnable command) {
        return createAlertDialog(context, title, message, R.string.ok, R.string.cancel, command);
    }

    // Two buttons dialog,点击取消有事件
    public static Dialog createAlertDialog(Context context, String title, String message, int confirmText,
                                           int cancelText, Runnable commandok, Runnable commandcancel) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        if (null != title)
            dialogBuilder.setTitle(title);
        dialogBuilder.setMessage(message);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(commandok));
        dialogBuilder.setNegativeButton(cancelText, new DialogCommandWrapper(commandcancel));
        return dialogBuilder.create();
    }

    // Three buttons dialog
    public static Dialog createAlertDialog3Btns(Context context, int title, int confirmText, int cancelText,
                                                int negText, Runnable command1, Runnable command2) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        dialogBuilder.setTitle(title);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command1));
        dialogBuilder.setNeutralButton(cancelText, new DialogCommandWrapper(NO_OP));
        dialogBuilder.setNegativeButton(negText, new DialogCommandWrapper(command2));
        return dialogBuilder.create();
    }

    // Three buttons dialog
    public static Dialog createAlertDialog3Btns(Context context, String title, String confirmText,
                                                String cancelText, String negText, Runnable command1, Runnable command2) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        dialogBuilder.setTitle(title);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command1));
        dialogBuilder.setNeutralButton(cancelText, new DialogCommandWrapper(NO_OP));
        dialogBuilder.setNegativeButton(negText, new DialogCommandWrapper(command2));
        return dialogBuilder.create();
    }

    // Three buttons dialog 带message
    public static Dialog createAlertDialog3Btns(Context context, String title, String message, String confirmText,
                                                String cancelText, String negText, Runnable command1, Runnable command2) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        dialogBuilder.setTitle(title);
        dialogBuilder.setMessage(message);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(command1));
        dialogBuilder.setNeutralButton(cancelText, new DialogCommandWrapper(NO_OP));
        dialogBuilder.setNegativeButton(negText, new DialogCommandWrapper(command2));
        return dialogBuilder.create();
    }

    // NO command dialog
    public static Dialog createAlertDialog(Context context, String title, String confirmText) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        dialogBuilder.setTitle(title);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(NO_OP));
        return dialogBuilder.create();
    }

    // NO command dialog
    public static Dialog createAlertDialog(Context context, int title, int confirmText) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
        dialogBuilder.setTitle(title);
        dialogBuilder.setPositiveButton(confirmText, new DialogCommandWrapper(NO_OP));
        return dialogBuilder.create();
    }

    /**
     * Static "Do Nothing" command when the user dismisses a dialog
     */
    public static final Runnable NO_OP = new Runnable() {
        @Override
        public void run() {
            // Do Nothing
        }
    };

    // Create item select dialog. 创建选择选项对话框。
    public static Dialog createItemSelectDialog(Context context, String title, CharSequence[] items,
                                                int checkedItem, OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setSingleChoiceItems(items, checkedItem, listener);//中间的参数代表默认第几项被选中,如果是-1,则没有选项被选中
        return builder.create();
    }

    // Create item select dialog. 创建选择选项对话框。
    public static Dialog createItemSelectDialog(Context context, int title, CharSequence[] items,
                                                int checkedItem, OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setSingleChoiceItems(items, checkedItem, listener);
        builder.setPositiveButton(R.string.ok, new DialogCommandWrapper(NO_OP));
        return builder.create();
    }

    // Create item select dialog. 创建选择选项对话框。
    public static Dialog createAdapterSelectDialog(Context context, String title, ListAdapter adapter, OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setAdapter(adapter, listener);
        return builder.create();
    }

    // Create item select dialog no title. 创建选择选项对话框没有标题。
    public static Dialog createItemSelectDialog(Context context, CharSequence[] items, int checkedItem,
                                                OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setSingleChoiceItems(items, checkedItem, listener);
        return builder.create();
    }

    // Create dialog with a chekcbox. 创建带复选框对话框
    public static Dialog createCheckboxDialog(Context context, CheckBox cb, int megId, int cbTextId,
                                              int posiTextId, int negaTextId, Runnable command) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setMessage(megId);
        cb.setText(cbTextId);
        builder.setView(cb);

        builder.setCancelable(false);
        builder.setPositiveButton(posiTextId, new DialogCommandWrapper(command));
        builder.setNegativeButton(negaTextId, new DialogCommandWrapper(NO_OP));
        return builder.create();
    }

    // Create dialog with a chekcbox. 创建带复选框对话框
    public static Dialog createCheckboxDialog(Context context, CheckBox cb, CharSequence msg, int cbTextId,
                                              int posiTextId, int negaTextId, Runnable command) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setMessage(msg);
        cb.setText(cbTextId);
        builder.setView(cb);

        builder.setCancelable(false);
        builder.setPositiveButton(posiTextId, new DialogCommandWrapper(command));
        builder.setNegativeButton(negaTextId, new DialogCommandWrapper(NO_OP));
        return builder.create();
    }

    // Create dialog with a chekcbox. 创建带编辑框对话框
    public static Dialog createEditextDialog(Context context, EditText et, CharSequence msg, int posiTextId,
                                             int negaTextId, Runnable command) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setMessage(msg);
        builder.setView(et);

        builder.setCancelable(false);
        builder.setPositiveButton(posiTextId, new DialogCommandWrapper(command));
        builder.setNegativeButton(negaTextId, new DialogCommandWrapper(NO_OP));
        return builder.create();
    }

    public static void showShortToastMessage(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static void showShortToastMessage(Context context, int messageId) {
        Toast.makeText(context, messageId, Toast.LENGTH_SHORT).show();
    }

}